version 16.1
clear all
cd "MYPATH\draft_tab_fig"
adopath + ../ado/

cap log close

log using rf_tables.log, replace

preliminaries
foreach PATH in RESULTS TEMP {
	cap mkdir "${`PATH'}\draft_tab_fig"
	cap mkdir "${`PATH'}\refine_key_figs"
    if "`PATH'" != "TEMP" cap mkdir "${`PATH'}\draft_tab_fig\figures"
}

program main
	key_tables
	cap log close
end

program key_tables
	use "$DATA\analysis_sample", clear
	label var did_nipt "Share got NIPT"
	label var did_invasive "Share got invasive" 
	
	gen post_nipt = 1*(wave == 3)
	keep if wave != 1
	
	qui {
		gen count_preg = 1
		gen risk = "vlow" if fetus_risk > 1000
		replace risk = "low" if fetus_risk <= 1000
		replace risk = "medium" if fetus_risk <= 200
		replace risk = "high" if fetus_risk < 51
	}
	
	* variables for regression
	local bin_size = 25
	qui gen fetus_risk_bin = .
	local count = 1
	forval bin_max = `bin_size'(`bin_size')20000 {
		local bin_inf = `bin_max' - `bin_size'
		qui replace fetus_risk_bin = `count' if fetus_risk <= `bin_max' & fetus_risk > `bin_inf'
		local count = `count' + 1
	}
	
	local bin_size = 5
	qui gen age_bin = .
	local count = 1
	forval bin_max = `bin_size'(`bin_size')80 {
		local bin_inf = `bin_max' - `bin_size'
		qui replace age_bin = `count' if age <= `bin_max' & age > `bin_inf'
		local count = `count' + 1
	}
	
	local tab_vars subt did_nipt did_invasive live healthy chroma no_live termination_clean stillbirth
	
	save "${TEMP}\draft_tab_fig\tab_samp.dta", replace 
	
	*********************************************************************************
	* Table of outcome shares by low, medium, high (dropping minimal)
	foreach gr in "51_200" "hr_cov" {
		foreach var in `tab_vars' {
		qui use "${TEMP}\draft_tab_fig\tab_samp.dta", clear 
			keep if nipt_`gr' == 1
			drop if risk == "vlow"
			if inlist("`var'", "live_birth", "healthy", "chroma", "no_live", ///
			  "termination_clean", "stillbirth") {
				keep if m_year < ym(2019,12)
			}
			collapse (mean) `tab_vars' (sum) count_preg, by(risk post_nipt)
			foreach r in "low" "medium" "high" {
				forval t = 0/1 {
					qui sum `var' if post_nipt == `t' & risk == "`r'"
					matrix temp_mat = nullmat(temp_mat), r(mean) 
				}
			}
			matrix shares_`gr' = nullmat(shares_`gr') \ temp_mat
			matrix drop temp_mat
		}	
		foreach r in "low" "medium" "high" {
			forval t = 0/1 {
				qui sum count_preg if post_nipt == `t' & risk == "`r'"
				matrix count_mat = nullmat(count_mat), r(mean) 
			}
		}
		matrix shares_`gr' = count_mat \ nullmat(shares_`gr')
		matrix drop count_mat
		frmttable using "${RESULTS}\draft_tab_fig\shares_`gr'.tex", statmat(shares_`gr') ///
		  rtitles("# of pregnancies" \ "Any post-NT testing" \ "cfDNA" \ "Invasive" \ ///
			"Live birth" \ "Live birth, no CA" \ "Live birth, CA" \ ///
			"No live birth" \ "Ended before 22 weeks" \ "Stillbirth")  ///
		  ctitles("" "Pre" "Post" "Pre" "Post" "Pre" "Post") ///
		  tex sdec(0 \ 2 \ 2 \ 2 \ 2 \ 2 \ 2 \ 2 \ 2) replace
		
		putexcel set "${RESULTS}\draft_tab_fig\shares_`gr'", replace
		putexcel A1 = matrix(shares_`gr'), nformat(number_d2)

	}
	
	************************************************************************************
	* Regression table testing difference between waves
	local reg_vars subt did_nipt did_invasive 
	foreach gr in "51_200" "hr_cov" {
		qui use "${TEMP}\draft_tab_fig\tab_samp.dta", clear 
		drop if fetus_risk > 1000
		keep if nipt_`gr' == 1
		foreach r in "low" {
			foreach var in `reg_vars' {
				replace `var' = `var' * 100
				qui sum `var' if post_nipt == 0 & risk == "`r'"
				local pre_mean = r(mean)
				local pre_n = r(N)
				qui sum `var' if post_nipt == 1 & risk == "`r'"
				local post_mean = r(mean)
				local post_n = r(N)
				
				qui reg `var' post_nipt if risk == "`r'", robust
				matrix `var'_coefse = `pre_mean', ., `post_mean', . , _b[post_nipt], _se[post_nipt]
				local n_noc = e(N)
				qui reg `var' post_nipt i.age_bin i.fetus_risk_bin if risk == "`r'", robust
				matrix `var'_coefse = `var'_coefse, _b[post_nipt], _se[post_nipt]
				local n_c = e(N)
				matrix `r'_mat = nullmat(`r'_mat) \ `var'_coefse
				
			}
			matrix `r'_mat = nullmat(`r'_mat) \ (`pre_n', ., `post_n', . , `n_noc', ., `n_c', .)
			frmttable using "${RESULTS}\refine_key_figs\diff_regs_`gr'", ///
			  statmat(`r'_mat) replace sdec(2 \ 2 \2 \ 2 \ 2 \ 2 \ 0) tex nodisplay ///
			  substat(1) ///
			  rtitles("Any post-KUB testing" \ "" \ "NIPT" \ "" \ ///
			  "Invasive" \ "" \ "N") ///
			  ctitles("" "Pre-NIPT" "Post-NIPT" "Diff: No controls" "Diff: Controls")
		}

		foreach r in "medium" "high" {
			if "`r" == "high" local dp = ""
			if "`r'" != "high" local dp "nodisplay"
			foreach var in `reg_vars' {
				qui sum `var' if post_nipt == 0 & risk == "`r'"
				local pre_mean = r(mean)
				local pre_n = r(N)

				qui sum `var' if post_nipt == 1 & risk == "`r'"
				local post_mean = r(mean)
				local post_n = r(N)
				
				qui reg `var' post_nipt if risk == "`r'", robust
				matrix `var'_coefse = `pre_mean', ., `post_mean', . ,_b[post_nipt], _se[post_nipt]
				local n_noc = e(N)
				
				qui reg `var' post_nipt i.age_bin i.fetus_risk_bin if risk == "`r'", robust
				matrix `var'_coefse = `var'_coefse, _b[post_nipt], _se[post_nipt]
				local n_c = e(N)
				matrix `r'_mat = nullmat(`r'_mat) \ `var'_coefse
			}
			matrix `r'_mat = nullmat(`r'_mat) \ (`pre_n', ., `post_n', . , `n_noc', ., `n_c', .)
			frmttable using "${RESULTS}\draft_tab_fig\diff_regs_`gr'.tex", ///
			  statmat(`r'_mat) merge sdec(2 \ 2 \2 \ 2 \ 2 \ 2 \ 0) tex ///
			  substat(1) title(`gr') ///
			  rtitles("Any post-KUB testing" \ "" \ "NIPT" \ "" \ ///
			  "Invasive" \ "" \ "N") ///
			  ctitles("" "Pre-NIPT" "Post-NIPT" "Diff: No controls" "Diff: Controls")
		}
		matrix all_mats = low_mat, medium_mat, high_mat
		
		putexcel set "${RESULTS}\draft_tab_fig\diff_regs_`gr'", replace
		putexcel A1 = matrix(all_mats), nformat(number_d2)
		
		matrix drop all_mats low_mat medium_mat high_mat

	}
	
end

* EXECUTE 
main
